﻿package net.wangit.adm.user;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionResult;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.DBFactory;
import net.wangit.support.Action;
import net.wangit.util.Md5Encoder;

public class ResetPasswordAction implements Action {

	private static final String SQL_UPDATE = "update AUSER set password=? where userid=?";

	public ResetPasswordAction() {
	}

	public ActionContext execute(ActionContext ac) throws Exception {
		String userId = ac.getRequestParameter("changepassword.userid");
		String newPassword = ac.getRequestParameter("changepassword.new");
		String confrimPassword = ac
				.getRequestParameter("changepassword.confirm");
		ActionResult result = ac.getActionResult();
		if (!confrimPassword.equals(newPassword)) {
			result.setSuccessful(false);
			String error = LabelUtil.get(ac, "adm",
					"resetpassword.confirm_failed");
			result.addError(error);
		} else {
			DB db = DBFactory.getDB();
			db.execute("update AUSER set password=? where userid=?",
					new String[] { Md5Encoder.encode(newPassword), userId },
					new int[] { 2, 2 });
			String info = LabelUtil.get(ac, "adm", "resetpassword.successful");
			result.addInfo(info);
		}
		return ac;
	}
}
